package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dto.ThirdTemplateDTO;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GwTemplate;
import com.ruoyi.system.service.IGwTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同模版管理Controller
 *
 * @author ruoyi
 * @date 2025-04-23
 */
@Api(description = "合同模版管理")
@RestController
@RequestMapping("/system/template")
public class GwTemplateController extends BaseController {
    @Autowired
    private IGwTemplateService gwTemplateService;

    /**
     * 查询合同模版管理列表
     */
    @ApiOperation("查询合同模版管理列表 templateType: 公司治理、员工关系、合同管理")
//    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(GwTemplate gwTemplate) {
        startPage();
        List<GwTemplate> list = gwTemplateService.selectGwTemplateList(gwTemplate);
        return getDataTable(list);
    }

    @ApiOperation("查询合同模版管理列表 templateType: 公司治理、员工关系、合同管理")
    @GetMapping("/getList")
    public AjaxResult getList(GwTemplate gwTemplate) {
        // 设置公司ID
        gwTemplate.setCompanyId(SecurityUtils.getLoginUser().getEnterpriseId());
        List<GwTemplate> list = gwTemplateService.selectGwTemplateList(gwTemplate);
        return success(list);
    }

    @ApiOperation("电子签认证或控制台")
    @GetMapping("/authenticationOrConsole")
    public AjaxResult authenticationOrConsole() {
        return gwTemplateService.authenticationOrConsole();
    }

    /**
     * 获取第三方应用模板列表
     */
    @ApiOperation("获取第三方应用模板列表")
    @PostMapping("/getThirdTemplateList")
    public AjaxResult getThirdTemplateList(@RequestBody ThirdTemplateDTO thirdTemplateDTO) throws TencentCloudSDKException {
        return gwTemplateService.getThirdTemplateList(thirdTemplateDTO);
    }

    /**
     * 获取第三方应用模板列表
     */
    @ApiOperation("获取第三方应用模板列表")
    @PostMapping("/getThirdTemplateListNoPage")
    public AjaxResult getThirdTemplateListNoPage(@RequestBody ThirdTemplateDTO thirdTemplateDTO) throws TencentCloudSDKException {
        return gwTemplateService.getThirdTemplateListNoPage(thirdTemplateDTO);
    }

    /**
     * 导出合同模版管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:template:export')")
    @Log(title = "合同模版管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GwTemplate gwTemplate) {
        List<GwTemplate> list = gwTemplateService.selectGwTemplateList(gwTemplate);
        ExcelUtil<GwTemplate> util = new ExcelUtil<GwTemplate>(GwTemplate.class);
        util.exportExcel(response, list, "合同模版管理数据");
    }

    /**
     * 获取合同模版管理详细信息
     */
    @ApiOperation("获取合同模版管理详细信息")
//    @PreAuthorize("@ss.hasPermi('system:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(gwTemplateService.selectGwTemplateById(id));
    }

    /**
     * 新增合同模版管理
     */
    @ApiOperation("新增合同模版管理 templateType: 公司治理、员工关系、合同管理")
//    @PreAuthorize("@ss.hasPermi('system:template:add')")
    @Log(title = "合同模版管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody GwTemplate gwTemplate) throws TencentCloudSDKException, IOException {
        return toAjax(gwTemplateService.insertGwTemplate(gwTemplate));
    }

    /**
     * 修改合同模版管理
     */
    @ApiOperation("修改合同模版管理")
//    @PreAuthorize("@ss.hasPermi('system:template:edit')")
    @Log(title = "合同模版管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody GwTemplate gwTemplate) throws IOException, TencentCloudSDKException {
        return toAjax(gwTemplateService.updateGwTemplate(gwTemplate));
    }

    /**
     * 删除合同模版管理
     */
    @ApiOperation("删除合同模版管理")
//    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "合同模版管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(gwTemplateService.deleteGwTemplateByIds(ids));
    }

    /**
     * 删除合同模版管理
     */
    @ApiOperation("删除合同模版管理")
//    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "合同模版管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/thirdRemove/{ids}")
    public AjaxResult thirdRemove(@PathVariable String[] ids) {
        return toAjax(gwTemplateService.thirdRemove(ids));
    }

}
